package com.qf.service.impl;

import com.qf.mapper.BranchMapper;
import com.qf.mapper.ProjectMapper;
import com.qf.mapper.StageMapper;
import com.qf.model.Branch;
import com.qf.model.Project;
import com.qf.model.Stage;
import com.qf.page.PageResult;
import com.qf.page.QueryPageBean;
import com.qf.service.BranchService;
import com.qf.service.StageService;
import com.qf.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class BranchServiceImpl implements BranchService {
    @Autowired
    private BranchMapper branchMapper;
    @Autowired
    private ProjectMapper projectMapper;

    @Override
    public List<Branch> getAll() {
        List<Branch> all = branchMapper.findAll();
        return all;
    }

    //分页查询+模糊查询
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer CurrentPage = queryPageBean.getCurrentPage();
        Integer PageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageResult pageResult = new PageResult();
        //当前页结果
        List<Branch> rows = branchMapper.findPage((CurrentPage-1)*PageSize, PageSize,queryString);
        //总记录数
        Long count = branchMapper.count(queryString);
        pageResult.setTotal(count);
        pageResult.setRows(rows);
        return pageResult;
    }

    //新建开发商并绑定工程
    @Override
    @Transactional
    public int add(Branch branch) {
        branch.setUpdatetime(new Date());
        branch.setBid(UUIDUtils.uuid());
        //新增stage
        int i = branchMapper.addOne(branch);
        return i;
    }


    //根据sid查询stage回显
    @Override
    public Branch findById(String bid) {
        Branch branch = branchMapper.findById(bid);

        return branch;
    }

    //修改branch
    @Override
    @Transactional
    public int edit(Branch branch) {
        branch.setUpdatetime(new Date());
        //修改developer
        int i = branchMapper.editOne(branch);
        return i;
    }

    //删除branch
    @Override
    @Transactional
    public int delete(String bid) {
        int i = branchMapper.delete(bid);
        return i;
    }
}
